
package com.scalar.chiptrack.operations.process.actions;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.operations.process.dao.AssemblyProcessSplitDAO;
import com.scalar.chiptrack.operations.process.dao.FabProcessSplitDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.dao.SortProcessSplitDAO;
import com.scalar.chiptrack.operations.process.dao.TestProcessSplitDAO;
import com.scalar.chiptrack.operations.process.forms.AssemblyProcessSplitForm;
import com.scalar.chiptrack.operations.process.forms.FabProcessSplitForm;
import com.scalar.chiptrack.users.User;


public final class AssemblyProcessSplitAction extends ChipTrackAction {

    public ActionForward execute (ActionMapping mapping, 
                                  ActionForm form, 
                                  HttpServletRequest request, 
                                  HttpServletResponse response) throws Exception 
	{
		String STEP_TYPE = "Assembly";
		HttpSession ses = request.getSession(true);
		String pageName = "AssemblyProcessSetUp";
		
		User user = (User) ses.getAttribute (SessionKeys.USER_KEY);
		if (user == null)
		{
			return (mapping.findForward ("login"));
		}
		String productId = (String)ses.getAttribute( "productId" );
		HashMap errors = new HashMap();
		FabProcessSplitDAO  fabProcessSplitDao     = new FabProcessSplitDAO();
		SortProcessSplitDAO sortProcessSplitDao    = new SortProcessSplitDAO();
		AssemblyProcessSplitDAO assemblyProcessSplitDao    = new AssemblyProcessSplitDAO();
		TestProcessSplitDAO testProcessSplitDao    = new TestProcessSplitDAO();

		HashMap assyHash = new HashMap();

		ArrayList testNameList = new ArrayList();
		ArrayList fabTrackList = (ArrayList)ses.getAttribute("fabTrackId");
		ArrayList assemblyTrackList = new ArrayList();

		String assemblyTrackId = "0";
		
		if( !isTokenValid(request) )
		{
			System.out.println("I am into reset Assembly ");
			
			FabProcessSplitDAO fabProcessSplitDAO = new FabProcessSplitDAO();
			TestProcessSplitDAO testProcessSplitDAO = new TestProcessSplitDAO();
			FabProcessSplitForm fabProcessSplitForm = null;
			
			fabProcessSplitForm = fabProcessSplitDAO.getFabTrackId(productId);
			String [] fabProduct = fabProcessSplitForm.getFabProduct();
			String [] fabTrackId=fabProcessSplitForm.getFabTrackId();
			AssemblyProcessSplitDAO assemblyProcessSplitDAO = new AssemblyProcessSplitDAO();
			AssemblyProcessSplitForm assemblyProcessSplitForm = null;
			assemblyProcessSplitForm = assemblyProcessSplitDAO.getAssemblyTrackId(fabTrackId);
			String [] assymbleyProduct = assemblyProcessSplitForm.getAssemblyProduct();
			String [] assemblyTrackId1 = assemblyProcessSplitForm.getAssemblyTrackId();

			ArrayList assemblyTrackList1 = new ArrayList();
			ArrayList testNameList1 = new ArrayList();
			HashMap assyHash1 = new HashMap();

			for( int i=0 ; i < assemblyTrackId1.length ; i++ )
			{
				assemblyTrackList1.add( assemblyTrackId1[i] );
				assyHash1.put( assemblyTrackId1[i] , assymbleyProduct[i] );
			}
			testNameList1 = testProcessSplitDAO.getTestNames();
			ses.setAttribute( "assemblyHash" , assyHash1 );
			ses.setAttribute( "testNames" , testNameList1 );
			ses.setAttribute( "assemblyTrackList" , assemblyTrackList1 );
			return (mapping.findForward("assemblysuccess")); 
		}	

		DBTransaction dbTransaction = null;

		String checkAssembly = assemblyProcessSplitDao.getAssembly( (String)fabTrackList.get(0) );	
		String [] fabProducts =(String [])ses.getAttribute("fabProduct");
		String [] assyProd = null;
		String dupNames = "";
		
		for( int i = 0 ; i < fabProducts.length ; i++ )
		{	
			if( fabProducts[i] != null && fabProducts[i].length() > 0 )
			{
				if( dupNames.length() > 0 )
				{ 
					dupNames = dupNames + "," + ProductDao.checkDupInProductMapTable( request.getParameterValues(fabProducts[i]) , -1 , STEP_TYPE );
				}
				else
				{
					dupNames = ProductDao.checkDupInProductMapTable( request.getParameterValues(fabProducts[i]) , -1 , STEP_TYPE );
				}
			}
		}
		//System.out.println("Dupnames "+ dupNames );
		if( dupNames.length() > 0 )
		{
			//System.out.println("I am having duplicates ");
			errors.put ( "Assembly name(s) already exists " , dupNames );
			request.setAttribute ( SessionKeys.ERROR_MESSAGES , errors );
			for( int i = 0 ; i < fabProducts.length ; i++ )
			{
				request.setAttribute( fabProducts[i] , request.getParameterValues( fabProducts[i] ) );					
			}
			return ( mapping.findForward("failure") );
		}
		
		boolean success = false;
		if(checkAssembly.equals("exists"))
		{
			assemblyProcessSplitDao.deleteAssembly(productId);
		}
		try
		{
			dbTransaction = DBTransaction.getInstance();
			dbTransaction.begin();

			for(int i=0;i<fabProducts.length;i++)
			{
				if(!fabProducts[i].equals(""))
				{
					assyProd = request.getParameterValues(fabProducts[i]) ;
					for(int j=0;j<assyProd.length;j++)
					{
						if(!assyProd[j].equals(""))
						{
							String fabTrackId = (String)fabTrackList.get(i);
							//System.out.println("fab prod "+fabProducts[i]+" assyprod "+assyProd[j]);
							assemblyTrackId=assemblyProcessSplitDao.save(dbTransaction,fabTrackId,assyProd[j],"Incomplete");
							//System.out.println("assemblyTrackId "+assemblyTrackId);
							assyHash.put(assemblyTrackId,assyProd[j]);
							assemblyTrackList.add(assemblyTrackId);
		     			}
				 	}
				}
			}
			success = true;
			if (success == true) 
			{
            	dbTransaction.commit();
				resetToken(request);
            }
            else 
            {
                    dbTransaction.rollback();
            }
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	
		testNameList = testProcessSplitDao.getTestNames();
		ses.setAttribute("assemblyHash",assyHash);
		ses.setAttribute("testNames",testNameList);
		ses.setAttribute("assemblyTrackList",assemblyTrackList);
		saveToken(request);
		return (mapping.findForward("assemblysuccess")); 
	}
}